package com.zsy.petshop_01.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsy.petshop_01.dao.mapper.VIPMessageMapper;
import com.zsy.petshop_01.dao.mapper.VIPVideoMapper;
import com.zsy.petshop_01.model.entity.User;
import com.zsy.petshop_01.model.entity.VIPMessage;
import com.zsy.petshop_01.model.entity.VIPVideo;
import com.zsy.petshop_01.model.vo.vip.PublishVideoVo;
import com.zsy.petshop_01.model.vo.vip.UpdateVideoVo;
import com.zsy.petshop_01.service.impl.MinIOService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Component
public class VIPManager {
    @Autowired
    VIPMessageMapper vipMessageMapper;
    @Autowired
    UserManager userManager;
    @Autowired
    VIPVideoMapper vipVideoMapper;
    @Autowired
    MinIOService minIOService;


    public List<VIPMessage> selectAll() {
        return vipMessageMapper.selectList(null);
    }

    public List<VIPMessage> selectByUserName(String userName) {
        LambdaQueryWrapper<VIPMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(VIPMessage::getUserName, userName);
        return vipMessageMapper.selectList(wrapper);
    }

    public void publish(String message, Integer userId) {
        VIPMessage vipMessage = new VIPMessage();
        vipMessage.setMessage(message);
        vipMessage.setUserId(userId);
        User user = userManager.selectById(userId);
        vipMessage.setUserName(user.getName());
        vipMessage.setCreateTime((double) System.currentTimeMillis());
        vipMessageMapper.insert(vipMessage);
    }

    public void delete(Integer id) {
        vipMessageMapper.deleteById(id);
    }

    public void publishVideo(PublishVideoVo publishVideoVo) {
        VIPVideo vipVideo = new VIPVideo();
        String videoName = publishVideoVo.getVideoName();
        vipVideo.setVideoName(videoName);
        MultipartFile video = publishVideoVo.getVideo();
        String minioPath = "";
        try {
            minioPath = minIOService.uploadFileInParts(video, videoName);
        }catch (Exception e) {
            e.printStackTrace();
        }
        vipVideo.setVideoUrl(minioPath);
        vipVideoMapper.insert(vipVideo);
    }

    public void updateVideo(UpdateVideoVo updateVideoVo){
        VIPVideo vipVideo = new VIPVideo();
        vipVideo.setId(updateVideoVo.getId());
        vipVideo.setVideoName(updateVideoVo.getVideoName());
        vipVideoMapper.updateById(vipVideo);
    }

    public void deleteVideo(Integer id) {
        vipVideoMapper.deleteById(id);
    }

    public List<VIPVideo> selectAllVideo(){
        return vipVideoMapper.selectList(null);
    }
}
